package com.shycloud.mido.member.service.impl;

import com.shycloud.mido.common.sms.sdk.CloopenSmsSDK;
import com.shycloud.mido.member.service.RegisterService;
import java.util.List;

import java.util.Map;
import lombok.SneakyThrows;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shycloud.mido.common.core.constant.CommonConstants;
import com.shycloud.mido.common.core.exception.BusinessException;
import com.shycloud.mido.member.api.entity.BaseMusicAuditionCode;
import com.shycloud.mido.member.api.entity.Member;
import com.shycloud.mido.member.mapper.BaseMusicAuditionCodeMapper;
import com.shycloud.mido.member.mapper.MemberMapper;
import com.shycloud.mido.member.mapper.MemberOrderMapper;
import com.shycloud.mido.member.service.BaseMusicAuditionCodeService;

import lombok.AllArgsConstructor;

@Service
@AllArgsConstructor
public class BaseMusicAuditionCodeServiceImpl extends
		ServiceImpl<BaseMusicAuditionCodeMapper, BaseMusicAuditionCode> implements
		BaseMusicAuditionCodeService {

	/** 有声书扫码激活业务 mapper */
	private BaseMusicAuditionCodeMapper auditionCodeMapper;
	/** 用户模块 mapper */
	private MemberMapper memberMapper;
	/** 用户模块 service */
	private MemberServiceImplV2 memberService;
	/** 会员注册 service */
	private RegisterService registerService;
	/** Redis 模块 */
	private RedisTemplate redisTemplate;
	/** 用户订单模块 mapper */
	private MemberOrderMapper memberOrderMapper;
	private final CloopenSmsSDK cloopenSmsSDK;

	/**
	 * 初始化判断有声书码是否使用过
	 *
	 * @param code
	 * @return java.lang.Boolean
	 * @author nianhua.jiang
	 * @date 2020/10/22 15:51
	 **/
	@Override
	public Boolean activateBookInit(String code) {

		//查询此码相关信息
		List<BaseMusicAuditionCode> baseMusicAuditionCodes = auditionCodeMapper.selectList(
				new QueryWrapper<BaseMusicAuditionCode>()
						.eq(BaseMusicAuditionCode.COL_AUDITION_CODE, code));

		if (baseMusicAuditionCodes.size() == 0) {
			throw new BusinessException("此二维码已失效");
		}

		if ("1".equals(baseMusicAuditionCodes.get(0).getCodeFlag())) {
			return false;
		}
		return true;
	}


	/**
	 * 注册并激活有声图书
	 *
	 * @param phone
	 * @param captcha
	 * @param code
	 * @return java.lang.Boolean
	 * @author nianhua.jiang
	 * @date 2020/10/22 16:06
	 **/
	@Override
	public Boolean activateBook(String phone, String captcha, String code) {

		//匹配验证码
		String code1 = (String) redisTemplate.opsForValue()
				.get(CommonConstants.DEFAULT_CODE_KEY + "SMS@" + phone);

		if (code1 == null) {
			throw new BusinessException("验证码已过期");
		}

		//校验验证码
		if (!code1.equals(captcha)) {
			throw new BusinessException("验证码不正确");
		}

		//注册会员 or 获取会员信息
		QueryWrapper<Member> query = new QueryWrapper<Member>();
		query.eq("username", phone).eq("del_flag", CommonConstants.STATUS_NORMAL);

		//判断是否注册会员，没注册则新注册用户
		Member member = memberMapper.selectOne(query);

		//注册会员
		if (member == null) {
			memberService.register(phone, "123456", null);
			member = memberMapper.selectOne(query);
		}

		//获取激活的曲集ID
		BaseMusicAuditionCode baseMusicAuditionCode = auditionCodeMapper.selectOne(
				new QueryWrapper<BaseMusicAuditionCode>()
						.eq(BaseMusicAuditionCode.COL_AUDITION_CODE, code));
		if (baseMusicAuditionCode == null) {
			throw new BusinessException("二维码已失效");
		}
		//校验是否已经购买了该曲集
		Integer isBuy = memberOrderMapper
				.checkIsByCollection(member.getId(), baseMusicAuditionCode.getMusicCollectionId());

		if (isBuy > 0) {
			throw new BusinessException("您已经激活了此图书，无需重复激活");
		}

		//追加用户-购买曲集 关系表
		memberOrderMapper.insertMemberBuyCollection(member.getId(),
				baseMusicAuditionCode.getMusicCollectionId(), "2", "0");
		baseMusicAuditionCode.setCodeFlag("1");
		auditionCodeMapper.updateById(baseMusicAuditionCode);
		return true;

	}

	@Override
	@SneakyThrows
	public boolean bindSyBook(String username, String bookNumber) {

		//注册用户(密码为手机号/账号)
		String memberId = registerService.syRegister(username);
		//通过上音社图书ID获取激活的曲集ID
		Map reqMap = auditionCodeMapper.getCollectionBySyId(bookNumber);

		Integer collectionId = (Integer) reqMap.get("id");
		String collName = (String) reqMap.get("name");

		if (collectionId == null) {
			throw new BusinessException("对应书籍不存在");
		}

		//发短信
		cloopenSmsSDK.sendTemplateSMS(username, "1090558", new String[]{collName});

		//追加用户-购买曲集 关系表
		memberOrderMapper.insertMemberBuyCollection(memberId, collectionId, "3", "1");
		return true;
	}

}
